/**
 * author      : dormi330
 * date        : 2018/7/11
 * description : zookeeper 实现的 分布式锁
 */

package com.wzq.learn.dlock;


import com.wzq.learn.dlock.facade.ILockService;
import com.wzq.learn.dlock.zk.ZkDistributedLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

public class LockServiceImpl implements ILockService {

    private String zkAddress;

    public LockServiceImpl(String ipPort) {
        this.zkAddress = ipPort;
    }

    @Override
    public Lock tryLock(String name, Long waitTime) {
        try {
            ZkDistributedLock lock = new ZkDistributedLock(zkAddress, name);
            lock.tryLock(waitTime, TimeUnit.MILLISECONDS);
            return lock;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

}